<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WebSocket Demo</title>
</head>
<body>
<h1>WebSocket Demo</h1>
<div>
  <input type="text" id="message" placeholder="输入消息">
  <button onclick="sendMessage()">发送</button>
</div>
<div id="chat"></div>

<script>
  let socket = new WebSocket("ws://localhost:8080/ws");

  socket.onopen = function () {
    console.log("WebSocket 连接成功");
  };

  socket.onmessage = function (event) {
    let chat = document.getElementById("chat");
    let message = document.createElement("p");
    message.textContent = event.data;
    chat.appendChild(message);
  };

  socket.onclose = function () {
    console.log("WebSocket 连接关闭");
  };

  function sendMessage() {
    let input = document.getElementById("message");
    socket.send(input.value);
    input.value = "";
  }
</script>
</body>
</html>
